package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.plugins.entity.Company;
import cn.jdy.plugins.entity.CompanyContext;
import cn.jdy.plugins.service.CompanyContextService;
import cn.jdy.plugins.service.CompanyService;

/**
 * 企业申请展示内容操作类
 * 
 * @author YangYi
 */
@Controller
@RequestMapping("/companyContext")
public class CompanyContextCtrl extends BaseCtrl {
  @Autowired
  private CompanyContextService companyContextService;
  @Autowired
  private CompanyService companyService;

  /**
   * 
   * @param typeid 品牌分类
   * @param pid 父id
   * @return
   */
  @RequestMapping("")
  public ModelAndView search() {
    ModelAndView mv = new ModelAndView(super.t + "/companyContext/productList");
    try {
      mv.addObject("bizClass", super.bizClass(24));
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "search", e);
    }
    return mv;
  }
  /**
   *
   * @param typeid 品牌分类
   * @param pid 父id
   * @return
   */
  @RequestMapping("/loadMore")
  @ResponseBody
  public Map<String, Object> loadMore(Integer iDisplayLength, Integer pageNo, String type) {
    Map<String, Object> resp = new HashMap<>();
    try {
      if (iDisplayLength == null || iDisplayLength.intValue() == 0)
        iDisplayLength = 10; // 默认长度10
      if (pageNo == null || pageNo.intValue() <= 0)
        pageNo = 1; // 默认页数1
      int displayStart = (pageNo - 1) * iDisplayLength; // 显示起始索引
      // 查询商机分类信息
      List<ConfValue> brandList = companyContextService.queryCompany(type, displayStart, iDisplayLength.intValue());
      if(brandList!=null && brandList.size()>0) {
        resp.put("brandList", brandList);
        resp.put("statusCode", 200);
      }else {
        resp.put("statusCode", 400);
      }

    } catch (Exception e) {
      BaseLog.e(this.getClass(), "search", e);
    }
    return resp;
  }

  /**
   * 品牌展示页面首页
   * 
   * @return
   */
  @RequestMapping("/index/{companyId}")
  public ModelAndView index(@PathVariable("companyId") Integer companyId) {
    ModelAndView mv = new ModelAndView(super.t + "/companyContext/productDetail");
    try {
      Company company = companyService.findOne("id", companyId);
      CompanyContext context = companyContextService.findOne("companyId", companyId+"");

      // 公司的联系方式
      /* 已 统一设置为 sjww 的客服电话 400-618-8728
      query.setParameter(1, company.getId());
      mv.addObject("mobile", memberService.getMobile());
      */
      
      mv.addObject("company", company);
      mv.addObject("context", context);
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "index 品牌展示页面首页失败", e);
    }

    return mv;
  }

}
